package com.bestdreamer.thread_safe;

public class MyObject2 {

    private static MyObject2 myObject2;

    private MyObject2(){}

    /**
     * 整个方法被上锁
     * 设置同步方法效率太低了
     */
    synchronized
    public static MyObject2 getInstance(){
        try{
            if(myObject2 != null){

            }else {
                // 模拟在创建对象之前做一些准备性的工作
                Thread.sleep(3000);
                myObject2 = new MyObject2();
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return myObject2;
    }
}
